﻿using System.Collections.Generic;
using System.Linq;
using Chambills.Present.Core;
using Chambills.Present.Core.NotifyOnChange;

namespace Chambills.Present.Validation
{
    /// <summary>
    /// If you bind a textbox to an integer proeprty, the binding breaks and the default value is used.
    /// This tells us which bindings are broken and hence type violatiosn
    /// </summary>
    public class BrokenBindingContributor : NotifiableObject, IViewModelValidationContributor
    {
        public BrokenBindingContributor()
        {
            BrokenBindings = new List<string>();
        }

        public List<string> GetCurrentErrors()
        {
            return BrokenBindings.Select(GetErrorMessage).ToList();
        }

        public List<string> GetErrorsForProperty(string name)
        {
            return BrokenBindings.Contains(name) ? new List<string> { GetErrorMessage(name) } : new List<string>();
        }

        public IViewModel Model { get; set; }

        private string GetErrorMessage(string name)
        {
            return name + " is invalid.";
        }

        public List<string> BrokenBindings { get; set; }
    }
}